﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PhotoUploader
{
    public delegate void RefreshConfigDelegate();

    public partial class ucConfigManager : UserControl
    {
        private string _configfile = Application.StartupPath + "\\Config\\" + "Interfaces.xml";

        private List<InterfaceConfig> ListInterfaces = new List<InterfaceConfig>();

        public event RefreshConfigDelegate ConfigRefreshed;
        private void OnConfigRefreshed()
        {
            if (ConfigRefreshed != null)
            {
                ConfigRefreshed();
            }
        }

        public ucConfigManager()
        {
            InitializeComponent();
            LoadInterface();
        }

        private void LoadInterface()
        {
            //配置文件路径
            InterfaceConfigReader<InterfaceConfig> reader = new InterfaceConfigReader<InterfaceConfig>(_configfile);
            //读配置
            if (!reader.Load())
            {
                MessageBox.Show("上传接口配置信息加载失败!\n" + reader.ErrorMessage);
            }
            ListInterfaces = reader.ListInterfaces;
            InitListBox();
        }

        private void InitListBox()
        {
            //实例化
            for (int i = 0; i < ListInterfaces.Count; i++)
            {
                InterfaceConfig config = ListInterfaces[i];
                checkedListBox1.Items.Add(config.InterfaceName, config.Enable);
            }        
        }

        private void UpdateConfigs()
        {
            List<InterfaceConfig> listConfigs = new List<InterfaceConfig>();
            listConfigs.AddRange(ListInterfaces);
            ListInterfaces.Clear();
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                string item = (string)checkedListBox1.Items[i];
                InterfaceConfig config = listConfigs.Find(r => r.InterfaceName.Equals(item));
                config.Enable = checkedListBox1.GetItemChecked(i);
                ListInterfaces.Add(config);
            }
            InterfaceConfigReader<InterfaceConfig> reader = new InterfaceConfigReader<InterfaceConfig>(_configfile);
            reader.UpdateConfigs(ListInterfaces);
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            UpdateConfigs();
            OnConfigRefreshed();
        }

        private void checkBoxSelectAll_CheckedChanged(object sender, EventArgs e)
        {    
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemChecked(i, checkBoxSelectAll.Checked);              
            }
        }

        private void buttonDown_Click(object sender, EventArgs e)
        {
            int max = checkedListBox1.Items.Count;
            int index = checkedListBox1.SelectedIndex;
            string itemContent = (string)checkedListBox1.SelectedItem;
            if (index == -1)
            {
                //MessageBox.Show("没有选中任何对象!");
                return;
            }
            if (index == max - 1)
            {
                //MessageBox.Show("已到达最底!");
                return;
            }
            checkedListBox1.Items.RemoveAt(index);
            checkedListBox1.Items.Insert(index + 1, itemContent);
            checkedListBox1.SelectedIndex = index + 1;
        }

        private void buttonUp_Click(object sender, EventArgs e)
        {
            int max = checkedListBox1.Items.Count;
            int index = checkedListBox1.SelectedIndex;
            string itemContent = (string)checkedListBox1.SelectedItem;
            if (index == -1)
            {
                //MessageBox.Show("没有选中任何对象!");
                return;
            }
            if (index == 0)
            {
                //MessageBox.Show("已到达最顶!");
                return;
            }
            checkedListBox1.Items.RemoveAt(index);
            checkedListBox1.Items.Insert(index - 1, itemContent);
            checkedListBox1.SelectedIndex = index - 1;
        }

        private void buttonAddPageConfig_Click(object sender, EventArgs e)
        {
            FormAddNewInterfaceConfig form = new FormAddNewInterfaceConfig();
            if (form.ShowDialog() == DialogResult.OK)
            {
                ListInterfaces.Add(form.Data);
                checkedListBox1.Items.Add(form.Data.InterfaceName, form.Data.Enable);
                InterfaceConfigReader<InterfaceConfig> reader = new InterfaceConfigReader<InterfaceConfig>(_configfile);
                reader.AddConfig(form.Data);
                OnConfigRefreshed();
            }
        }
    }
}
